// pages/createPatient/contact.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isOther: true,
		isEdit: '',
		patientData: {
			age: '',
			allergy: "",
			birthday: "",
			contact: "",
			contactRelationType: "",
			contactTel: "",
			familyHistory: "",
			idCard: "",
			nation: "",
			pastHistory: "",
			patientName: "",
			personalHistory: "",
			postnatalFactors: null,
			prenatalFactors: null,
			productionFactors: null,
			relation: "",
			sex: ''
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		const isEdit = JSON.parse(options.data)
		let data = app.globalData.changePatientInfo
		switch (data.contactRelationType) {
			case '父母':
				data.contactRelationType = 5
				break;
			case '祖父母':
				data.contactRelationType = 6
				break;
			case '外祖父母':
				data.contactRelationType = 7
				break;
			case '其他':
				data.contactRelationType = 9
				break;
			default:
		}
		this.setData({
			patientData: data,
			isEdit: isEdit.isEdit
		})
	},
	contactTypeChange(e) {
		let isOther = this.data.isOther
		let other = this.data.patientData.relation
		if (e.detail.value === '9') {
			isOther = false
		} else {
			isOther = true
			other = ''
		}
		let patientData = this.data.patientData
		patientData.contactRelationType = Number(e.detail.value)
		isOther = isOther
		patientData.relation = other
		this.setData({
			patientData: patientData,
			isOther: isOther
		})
	},
	// 其他
	relation(e) {
		let patientData = this.data.patientData
		patientData.relation = e.detail.value
		this.setData({
			patientData: patientData
		})
	},
	// 联系人
	contact(e) {
		let patientData = this.data.patientData
		patientData.contact = e.detail.value
		this.setData({
			patientData: patientData
		})
	},
	// 联系电话
	contactTel(e) {
		let patientData = this.data.patientData
		patientData.contactTel = e.detail.value
		this.setData({
			patientData: patientData
		})
	},
	next() {
		const contactTelNo = /^1\d{10}$/;
		const patientData = this.data.patientData
		if (!patientData.contact) {
			wx.showToast({
				title: '请填写联系人姓名',
				icon: "none",
			})
			return
		}
		if (!contactTelNo.test(patientData.contactTel)) {
			wx.showToast({
				title: '请填写正确的手机号',
				icon: "none",
			})
			return
		}
		if (!patientData.contactRelationType) {
			wx.showToast({
				title: '请选择联系人关系',
				icon: "none",
			})
			return
		}
		if (patientData.contactRelationType === 9 && !patientData.relation) {
			wx.showToast({
				title: '请输入患者关系',
				icon: "none",
			})
			return
		}
		app.globalData.changePatientInfo = patientData
		wx.navigateTo({
			url: './other?isEdit=' + this.data.isEdit
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
